<template>
	<view class="form">
		<view class="cu-form-group">
			<view class="title">原密码</view>
			<input type="password" placeholder="请输入原密码" v-model="oldPassword"></input>
		</view>
		<view class="cu-form-group border">
			<view class="title">新密码</view>
			<input type="password" placeholder="请输入新密码" v-model="newPassword"></input>
		</view>
		<view class="cu-form-group border">
			<view class="title">再次输入</view>
			<input type="password" placeholder="请再次输入密码" v-model="againPassword"></input>
		</view>
		<button class="cu-btn btn-default btn" @tap="changePassword()">重置</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldPassword: '',
				newPassword: '',
				againPassword: '',
			};
		},
		methods: {
			changePassword() {
				// console.log(getApp().globalData.userData);
				if(this.oldPassword == '') {
					uni.showToast({
						duration: 2000,
						title: '请输入原密码',
						icon: 'none'
					})
					return
				}
				if(this.newPassword == '') {
					uni.showToast({
						duration: 2000,
						title: '请输入新密码',
						icon: 'none'
					})
					return
				}
				if(this.againPassword == '') {
					uni.showToast({
						duration: 2000,
						title: '请再次输入新密码',
						icon: 'none'
					})
					return
				}
				if(this.newPassword !== this.againPassword) {
					uni.showToast({
						duration: 2000,
						title: '两次输入的密码不同',
						icon: 'none'
					})
					return
				}
				let token = getApp().globalData.token
				let userId = getApp().globalData.userData.id;
				uni.request({
					url: getApp().globalData.url + '/blade-auth/oauth/token',
					header: {
						'blade-auth': "bearer " + token
					},
					method: "post",
					data: {
						userId: userId,
						oldPassword: this.oldPassword,
						newPassword: this.newPassword,
						newPassword1: this.againPassword,
						tenantId: '000000',
					},
					success: (res) => {
						console.log(res);
						let code = res.data.code;
						if (code == 200) {
							uni.showToast({
								duration: 2000,
								title: '密码修改成功，需要重新登录',
								icon: 'none'
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '../login/login'
								})
							})
						} else {
							uni.showToast({
								duration: 2000,
								title: '修改失败',
								icon: 'none'
							})
						}
					}
				});
			}
		}
	}
</script>

<style>
	.btn {
		width: calc(100% - 60rpx);
		margin: 80rpx 30rpx 0;
	}
</style>
